import random

from BasicDefinition import *
from GeneticAlgorithm import Individual


def Greedy_Decision(network: Network, cx, cy, block_idx, Seq_Length):
    scheme_seq = []
    Current_Satellite = network.SatelliteGroup[cx][cy]
    tasks = [Current_Satellite.TaskList[idx] for idx in Current_Satellite.TaskBlock[block_idx]]
    Satellite_list = network.GetAdjSatellites(cx, cy)
    offbias = [[0 for _ in range(network.SatelliteNumber)] for __ in range(network.OrbitNumber)]
    while len(scheme_seq) < Seq_Length:
        MaxResidual, tar = -1, -1
        # random.shuffle(Satellite_list)
        for satellite in Satellite_list:
            row, col = network.Inv_CoordinationMapping(satellite)
            if network.Get(satellite).ResidualResource() - MaxResidual - offbias[row][col] > 0.01:
                MaxResidual = network.Get(satellite).ResidualResource() - offbias[row][col]
                tar = satellite
                offbias[row][col] += sum([task.Slice[len(scheme_seq)] for task in tasks])
        scheme_seq.append(tar)
    return Individual(scheme_seq, 0, 0, 0, cx, cy, network, block_idx)

# for i in range(1,20):
#     network = Network(8, 8,
#                       4, 3,
#                       8, i,
#                       200, 40,
#                       4, 400,
#                       1000, 2)
#     x = EventLine(network, Greedy_Decision, 3)
#     # y = EventLine(network, GA_Decision, 40, 5, 20, pow(10, 7), 1, 20, 100, 20)
#     x.Simulate()
#     # for orbit in network.SatelliteGroup:
#     #     for s in orbit:
#     #         print('{0: <5}'.format(round(1 - s.ResidualResource(), 3)), end=" ")
#     #     print(" ")
#     # y.Simulate()
#     # y.Simulate()
#     print("Greedy:", x.TotalDrop, x.TotalMission)
# # print("GA:", y.TotalDrop, y.TotalMission)
